﻿;

function SetListHtml(oParameter) {
    if (!SetListHtml._initialize) {
        $.extend(SetListHtml.prototype, IDataClass, IEventClass, {
            changeData: function(oData, i) {
                var _ = this;
                var aData = _.get("ValueData");
                aData[i] = oData;
                _.setHtml(i, i + 1);
                return this;
            },
            setData: function(oData, bIsAll) {
                var _ = this;
                if (bIsAll) {
                    _.set("ValueData", oData == null ? [] : oData instanceof Array ? oData : [oData]);
                    _.trigger("setListAll", _.get("ValueData"));
                    _.setHtml();
                } else {
                    var aData = _.get("ValueData");
                    if (!aData || oData == null) {
                        aData = [];
                        _.set("ValueData", aData);
                    }
                    if (oData == null) {
                        _.setHtml();
                        return this;
                    }
                    var index = aData.length;
                    if (oData instanceof Array) {
                        _.set("ValueData", aData.concat(oData));
                    } else {
                        aData.push(oData);
                    }
                    _.setHtml(index);
                }
                return this;
            },
            setHtml: function(iBegin, iEnd) {
                var _ = this;
                var aData = _.get("ValueData");
                var $list = _.get("$list", true);
                if (!iBegin || iBegin < 0) {
                    iBegin = 0;
                }
                if (!iEnd || iEnd < 0) {
                    iEnd = $list && $list.length > aData.length ? $list.length : aData.length;
                }
                if (iBegin > iEnd) {
                    var item = iBegin;
                    iBegin = iEnd;
                    iEnd = item;
                }
                for (var i = iBegin; i < iEnd; i++) {
                    var oItem = aData[i];
                    if (oItem == null) {
                        if ($list && i < $list.length) {
                            $list.eq(i).hide();
                        }
                        continue;
                    }
                    var $m;
                    if (!$list || i >= $list.length) {
                        var $clone = _.get("$clone", true);
                        $m = $clone.clone().show();
                        if ($list && $list.length > 0) {
                            $list.last().after($m);
                        } else {
                            _.get("$", true).append($m);
                        }
                        _.trigger("cloneItem", $m, oItem, i);
                    } else {
                        $m = $list.eq(i).show();
                    }
                    _.trigger("setListItem", $m, oItem, i);
                    $list = _.get("$list", true);
                }
                if (aData.length > 0) {
                    $list.filter(":gt(" + (aData.length - 1) + ")").hide();
                } else {
                    $list.hide();
                }
                _.trigger("setListEnd", aData, $list);
                return this;
            },
        });
        SetListHtml._initialize = true;
    }
    var obj = this;
    //参数
    obj._parameter = $.extend({
        //容器
        $: undefined,
        //副本[Function,jQuery]{parameter:[SetListHtml[this]]}
        $clone: function () {
            return this.get("$list", true).last();
        },
        //列表[Function]{parameter:[SetListHtml[this]]}
        $list: function () {
            return this.get("$", true).children();
        },

        //setListAll[Function]{explain:设置所有项开始事件,parameter:[data:{aData:列表数据}]}
        //setListEnd[Function]{explain:设置列表结束事件,parameter:[data:{aData:列表数据,$list:元素列表}]}
        //setListItem[Function]{explain:设置列表项事件,parameter:[data:{$m:项元素,oItem:项数据,index:下标}]}
    }, oParameter);
    obj._configuration = {};
    obj._dataList = {};
    obj._eventList = {};
    return obj;
}

;
(function ($, window, undefined) {

    $.fn.setListHtml = function (oParameter) {
        var _ = $(this);
        var slh = _.data("SetListHtml");
        if (slh) {
            return slh;
        }
        slh = new SetListHtml($.extend({ $: _ }, oParameter));
        _.data("SetListHtml", slh);
        return slh;
    };
})(jQuery, window);